#include "GTTcpConnectionForImageReceive.h"
#include <iostream>
#include <string.h>
#include "global.h"
#include <fstream>

GTTcpConnectionForImageReceive::GTTcpConnectionForImageReceive(const sockaddr_in& stClientSocket, int nNewSock)
:GTTcpConnection(stClientSocket, nNewSock),
m_uDataLength(0),
m_bImgMsg(false),
m_bImgDataFullyReceived(false)
{

}

GTTcpConnectionForImageReceive::~GTTcpConnectionForImageReceive()
{

}

void GTTcpConnectionForImageReceive::NewDataArrived(const char* pData, unsigned int nDataLen)
{
       
    //std::cout<< "GTTcpConnectionForImageReceive::NewDataArrived" << std::endl;
    //std::cout<< nDataLen << ": " << pData << std::endl;
    //std::cout<< pData << std::endl;
    
    if(m_uDataLength + nDataLen < DATA_BUFFER_SIZE)
    {
        memcpy(m_arrBuffer + m_uDataLength, pData, nDataLen);
        m_uDataLength += nDataLen;
    }
    
    // handle received data.
    if(m_uDataLength >= 4)
    {
        int nMsgType;
        memcpy(&nMsgType, m_arrBuffer, sizeof(int));

        if(4001 == nMsgType)
        {
            unsigned int uUnhandledDataLen = m_uDataLength - sizeof(int);
            char* pUnhandledData = new char[uUnhandledDataLen];
            memcpy(pUnhandledData, m_arrBuffer+sizeof(int), uUnhandledDataLen);

            memset(m_arrBuffer, '\0', DATA_BUFFER_SIZE);

            memcpy(m_arrBuffer, pUnhandledData, uUnhandledDataLen);
            m_uDataLength = uUnhandledDataLen;

            delete [] pUnhandledData;
            std::cout<< "heart beat msg." << "Unhandled Data len: " << uUnhandledDataLen<<std::endl;
        }
        else if(4002 == nMsgType)
        {
            m_bImgMsg = true;
            
            _ImageDataHeader stImgHeader;
            if(m_uDataLength >= sizeof(int)+ sizeof(_ImageDataHeader))
            {
                memcpy(&stImgHeader, m_arrBuffer+sizeof(int), sizeof(_ImageDataHeader));
                //std::cout<< stImgHeader.timeStamp << std::endl;
                int nMsgTotalLen = sizeof(int) + sizeof(_ImageDataHeader) + stImgHeader.imgLen;
                if(m_uDataLength >= nMsgTotalLen)
                {
                    std::cout<< "Image msg." <<std::endl;
                    // handle msg.
                    std::string strFileName = std::string(stImgHeader.timeStamp)+"."+std::string(stImgHeader.imgType);
                    
                    std::ofstream ofile;               //定义输出文件
                    ofile.open("myfile.txt", std::ios::app);     //作为输出文件打开
                    ofile<<strFileName << std::endl;   //标题写入文件



#if 0
                     std::fstream file(strFileName, std::ios::out | std::ios::binary);
                     if (!file)
                     {
                         std::cout << "Error opening file." << std::endl;
                     }

                     int nOffset = sizeof(int) + sizeof(_ImageDataHeader);
                     file.write(m_arrBuffer + nOffset, stImgHeader.imgLen);
                     file.close ();
#endif               








                    // clear
                    unsigned int unHanledDataLen = m_uDataLength - nMsgTotalLen;
                    char* pUnhandledData = new char[unHanledDataLen];

                    memcpy(pUnhandledData, m_arrBuffer+nMsgTotalLen, unHanledDataLen);

                    memset(m_arrBuffer, '\0', DATA_BUFFER_SIZE);

                    memcpy(m_arrBuffer, pUnhandledData, unHanledDataLen);

                    m_uDataLength = unHanledDataLen;

                    delete [] pUnhandledData;

                    std::cout<< "Image msg." << "Unhandled Data len: " << unHanledDataLen<<std::endl;

                }
                else
                {
                    return;                    
                }


            }
            else 
            {                
                return;
            }
        }
    }

}